package com.one.shopping.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.one.shopping.R;
import com.one.shopping.api.OneApi;
import com.one.shopping.entity.Goods;
import com.one.shopping.util.CommonUtils;
import com.one.shopping.util.MyClick;

import java.util.List;

/**
 * Created by Administrator on 2016/1/13.
 */
public class ShopGoodsAdapter extends BaseAdapter{
    private Context context;
    private List<Goods> list;
    private MyView view;
    private int contextType;
    public static final int GOODS=0;
    public static final int SHOPPINGCART=1;

    public ShopGoodsAdapter(Context context, List<Goods> list,int contextType) {
        this.context = context;
        this.list = list;
        this.contextType=contextType;
    }

    @Override
    public int getCount() {
        // TODO 自动生成的方法存根
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO 自动生成的方法存根
        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO 自动生成的方法存根
        return arg0;
    }

    @Override
    public View getView(final int arg0, View converView, ViewGroup arg2) {

        if(converView==null){
            //没有找过布局没有找过组件
            LayoutInflater inflater=LayoutInflater.from(context);
            converView=inflater.inflate(R.layout.shoppingcart_item,null);
            view=new MyView();
            view.iv=(ImageView) converView.findViewById(R.id.item_image);
            view.tV_id=(TextView) converView.findViewById(R.id.item_id);
            view.tV_name=(TextView) converView.findViewById(R.id.item_name);
            view.tV_price=(TextView) converView.findViewById(R.id.item_price);
            view.tV_stock=(TextView) converView.findViewById(R.id.item_stock);
            view.et_quantity=(EditText) converView.findViewById(R.id.item_quantity);
            view.btn_jia=(Button) converView.findViewById(R.id.jia_btn);
            view.btn_jian=(Button) converView.findViewById(R.id.jian_btn);
            converView.setTag(view);
        }else{
            view=(MyView) converView.getTag();
        }
        switch (contextType) {
            case GOODS:
                //所有商品页面的列表
//                view.iv.setImageResource(R.mipmap.ic_launcher);//获取商品image   list.get(arg0).getImage()
                CommonUtils.setImageForUrl(view.iv, OneApi.IMAGE_URL + list.get(arg0).getImage());
                view.tV_id.setText("商品编号: "+list.get(arg0).getId());
                view.tV_name.setText("商品名称: "+list.get(arg0).getName());
                view.tV_price.setText("单价(￥): "+list.get(arg0).getPrice());
                view.tV_stock.setText("库存量: "+list.get(arg0).getStock()+"");
                view.et_quantity.setText(QueryQuantity(list.get(arg0).getId()));
                view.btn_jia.setOnClickListener(new MyClick(context,list.get(arg0), view.et_quantity,0,null,0));
                view.btn_jian.setOnClickListener(new MyClick(context,list.get(arg0),view.et_quantity,1,null,0));

                break;

            case SHOPPINGCART:
                //购物车页面的列表
//                view.iv.setImageResource(R.mipmap.ic_launcher);//获取商品image   list.get(arg0).getImage()
                CommonUtils.setImageForUrl(view.iv, OneApi.IMAGE_URL + list.get(arg0).getImage());
                view.tV_id.setText("商品编号: "+list.get(arg0).getId());
                view.tV_name.setText("商品名称: "+list.get(arg0).getName());
                view.tV_price.setText("单价(￥): "+list.get(arg0).getPrice());
                view.tV_stock.setText("库存量: "+list.get(arg0).getStock()+"");
                view.et_quantity.setText(QueryQuantity(list.get(arg0).getId()));
                view.btn_jia.setOnClickListener(new MyClick(context,list.get(arg0), view.et_quantity,0,null,0));
                view.btn_jian.setOnClickListener(new MyClick(context,list.get(arg0),view.et_quantity,1,this,arg0));
                break;

            default:
                break;
        }
        return converView;


    }

    private void remove(int arg0) {
        // 实例化SharedPreferences对象
        SharedPreferences mySharedPreferences = context.getSharedPreferences(
                "shoppingcart", Activity.MODE_PRIVATE);
        // 实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        String id = mySharedPreferences.getString("id", "");
        String name = mySharedPreferences.getString("name", "");
        String image = mySharedPreferences.getString("image", "");
        String price = mySharedPreferences.getString("price", "");
        String quantity = mySharedPreferences.getString("quantity", "");

        String ids[] = id.split(",");
        String names[] = name.split(",");
        String images[] = image.split(",");
        String prices[] = price.split(",");
        String quantitys[] = quantity.split(",");

        for (int i = 0; i < ids.length; i++) {
            if (ids[i].equals(list.get(arg0).getId() + "")) {
                for (int j = 0; j < ids.length-1; j++) {
                    if (i==0) {
                        id=id+","+ids[j+1];
                        name=name+","+names[j+1];
                        image=image+","+images[j+1];
                        price=price+","+prices[j+1];
                        quantity=quantity+","+quantitys[j+1];
                    }else{
                        if (j<i) {
                            if (j==0) {
                                id=ids[j];
                                name=names[j];
                                image=images[j];
                                price=prices[j];
                                quantity=quantitys[j];
                            }else{
                                id=id+","+ids[j];
                                name=name+","+names[j];
                                image=image+","+images[j];
                                price=price+","+prices[j];
                                quantity=quantity+","+quantitys[j];
                            }
                        }else{
                            id=id+","+ids[j+1];
                            name=name+","+names[j+1];
                            image=image+","+images[j+1];
                            price=price+","+prices[j+1];
                            quantity=quantity+","+quantitys[j+1];
                        }
                    }

                }
                editor.putString("id", id + "");
                editor.putString("name", name);
                editor.putString("image", image);
                editor.putString("price", price);
                editor.putString("quantity", quantity);
                editor.commit();
                return;
            }
        }
    }

    /**
     * 根据ID查询商品加入购物车数量
     * @param goodid
     * @return
     */
    public String QueryQuantity(String goodid){
        String goodQuantity="0";
        //实例化SharedPreferences对象
        SharedPreferences mySharedPreferences= context.getSharedPreferences("shoppingcart", Activity.MODE_PRIVATE);
        String id = mySharedPreferences.getString("id","");
        if (id!="") {
            String quantity = mySharedPreferences.getString("quantity", "");
            String ids[] = id.split(",");
            String quantitys[] = quantity.split(",");
            for (int i = 0; i < ids.length; i++) {
                if (ids[i].equals(goodid+"")) {//判断id是否已经存在
                    goodQuantity=quantitys[i];//找出ID 对应的数量
                    return goodQuantity;
                }
            }

        }

        return goodQuantity;
    }
    /**
     * 删除一行
     * @param index
     */
    public void deleteItem(int index) {
        list.remove(index);
        notifyDataSetChanged();
    }
}


class MyView{
    public ImageView iv;
    public TextView tV_id;
    public TextView tV_name;
    public TextView tV_price;
    public TextView tV_stock;
    public EditText et_quantity;
    public Button btn_jia;
    public Button btn_jian;
}